<?php
namespace app\api\controller\login;

use app\api\controller\Member;
use app\common\model\CourseAttendanceModel;
use app\common\model\CourseModel;
use app\common\model\SchoolClassModel;
use app\common\model\SchoolGradeModel;
use app\common\model\UserModel;
use think\db;

class Schoolmaster extends Member
{

    /**
     * 校长的话计算学生数与班级数与缺勤率
     */
    public function getTimeCount(){
        $user = $this->getMember();
        $result['student_count'] = UserModel::where('school_id',$user['school_id'])
            ->where('role',1)
            ->where('student_no is not null and student_no !=""')
            ->count();

//        $result['class_count'] =  UserModel::where('school_id',$user['school_id'])
//            ->where('role', 1)
//            ->where('class_id','neq',0)
//            ->where('grade_id','neq',0)
//            ->group('class_id,grade_id')
//            ->count();
        $result['class_count'] = CourseModel::where('school_id', $user['school_id'])->count();

         $result['class'] =  UserModel::where('school_id',$user['school_id'])
            ->where('class_id','neq',0)
            ->where('grade_id','neq',0)
            ->field('class_id,grade_id')
            ->group('class_id,grade_id')
            ->select();
        //缺勤率
        $course_ids = CourseModel::where('school_id',$user['school_id'])
            ->field('group_concat(id) as course_ids')->find()['course_ids'];
        $attendance_count = CourseAttendanceModel::where('course_id','in',$course_ids)
            ->where('away_userids','neq',null)
            ->where('all_userids','neq',null)
            ->count();
        $attendance = CourseAttendanceModel::where('course_id','in',$course_ids)->select();
        $rate = 0;
        foreach ($attendance as $key=>&$value){
            if($value['away_userids']!=null&&$value['all_userids']!=null&&count(explode(',',$value['all_userids']))!=0){
                $rate += count(explode(',',$value['away_userids']))/count(explode(',',$value['all_userids']));
            }
        }
        if($attendance_count!=0){
            $attendance_rate = $rate/$attendance_count;
        }else{
            $attendance_rate = 0;
        }
        $result['attendance_rate'] = round($attendance_rate,4)*100;
        $this->success('获取校长的学生数与班级数与缺勤率成功！',$result,200);
    }

    /**
     * 校长的话计算缺勤详细情况
     */
    public function getAttendance(){
        $user = $this->getMember();
        //得到学校所有的班级的课程
        $course_ids = CourseModel::where('school_id',$user['school_id'])
            ->field('group_concat(id) as course_ids')->find()['course_ids'];
        //得到考勤列表
        $attendance = CourseAttendanceModel::where('course_id','in',$course_ids)
            ->order('create_time desc')->select();
        //循环拼成数组
        foreach ($attendance as $key=>&$value){
            $all_usernames = '';
            $away_usernames = '';

            $value['key'] = $key+1;
            $course = CourseModel::where('id',$value['course_id'])->find();
            $value['course_name'] = $course['name'];
            $value['address'] = $course['address'];
            $value['teacher_name'] = UserModel::where('id',$course['teacher_id'])->value('realname');
            $value['class_name'] = SchoolClassModel::where('id',$course['class_id'])->value('name');
            $value['grade_name'] = SchoolGradeModel::where('id',$course['grade_id'])->value('name');
            $all_userids_arr = explode(',',$value['all_userids']);
            foreach ($all_userids_arr as $k1=>$v1){
                //报名的学生
                $all_usernames = $all_usernames .' '.UserModel::where('id',$v1)->value('realname');
            }
            $away_userids_arr = explode(',',$value['away_userids']);
            foreach ($away_userids_arr as $k2=>$v2){
                //缺勤的学生
                $away_usernames = $away_usernames .' '.UserModel::where('id',$v2)->value('realname');
            }
            $value['all_usernames'] = $all_usernames;
            $value['away_usernames'] = $away_usernames;
            $value['all_usernames_count'] = empty($value['all_userids']) ? 0 : count($all_userids_arr);
            $value['away_usernames_count'] = empty($value['away_userids']) ? 0 : count($away_userids_arr);
            $value['absence_rate'] = empty($value['away_userids'])?'0%':(round(count($away_userids_arr)/count($all_userids_arr),4)*100).'%';
        }
        $this->success('获取校长所有班级的缺勤详细情况成功！',$attendance,200);
    }

    /**
     * 获取班主任的学生
     */
    public function getStudent()
    {
        $user = $this->getMember();
//        $student = [];
//        if($user['school_id'] > 0){
//            $student = UserModel::where('school_id',$user['school_id'])
//                ->where('role',1)->select();
//        }
        $student = UserModel::with(['schoolgrade', 'schoolclass'])
            ->join('edu_user_times t','t.user_id=edu_user.id','LEFT')
            ->join('edu_course c','t.course_id=c.id','LEFT')
            ->join('edu_school_class clas','clas.id=edu_user.class_id','LEFT')
            ->join('edu_user u2','u2.id=edu_user.pid','LEFT')
            ->where('edu_user.school_id', $user['school_id'])
            ->where('edu_user.role', 1)
            ->field('edu_user.*,u2.realname AS bind_name, GROUP_CONCAT(DISTINCT c.name) as course_name')
            ->group('edu_user.id')
            ->all();

        // 过滤学号为空
        $result = [];
        foreach ($student as $key => $item)
        {
            if(!empty($item['student_no'])){
                $result[] = $item;
            }
        }
        $student = $result;

        $this->success('获取校长的学生成功！',$student,200);
    }

    /**
     * 获取班级数
     */
    public function getClass()
    {
        $user = $this->getMember();
        $class = [];
        if(!empty($user['school_id']))
        {
            $class = CourseModel::where('school_id',$user['school_id'])->with('teacher')->all();
        }

        $this->success('获取校长的班级成功！',$class,200);
    }

}